package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author alexis
 * @date 2024-11-03
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
//        先查护理计划表
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
//        把nursingPlan封装到NursingPlanVo中
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
//        在查护理项目计划表
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlanMapper.selectByPlanId(nursingPlanVo.getId());
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
//        1.保存护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();

//        2.对象属性的拷贝
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
//        给nursingPlan 创建时间赋值
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
//批量保存护理计划和护理项目的对应关系
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
//        获取护理计划id
        projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));
        int result = nursingProjectPlanMapper.BatchInsert(projectPlans);
        return result > 0 ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
//        1.把nursingPlanDto拆分成nursingPlan对象
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
//        2.更新护理计划表
        nursingPlanMapper.updateById(nursingPlan);
//        3.删除NursingPlanDto中的projectPlans 再新增
        nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());
//        4.获取projectPlans
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
//        给projectPlans赋值护理计划id
        projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(nursingPlan.getId()));
//        5.批量添加
        int result = nursingProjectPlanMapper.BatchInsert(projectPlans);
        return result > 0 ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long[] ids) {
        boolean b = removeByIds(Arrays.asList(ids));
        nursingProjectPlanMapper.deleteByPlanId(ids[0]);
        return b ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }
}
